package com.pippin.editor.liteflow.condition;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.pippin.editor.liteflow.ConditionParse;
import com.pippin.editor.liteflow.ElParseFactory;
import com.yomahub.liteflow.flow.element.condition.ConditionKey;

import java.util.Objects;
import java.util.stream.Collectors;

public class PreCondition extends ConditionParse {
    @Override
    public String buildEl(JSONObject json) {
        JSONObject jsonObject = json.getJSONObject("executableGroup");
        JSONArray jsonArray = jsonObject.getJSONArray(ConditionKey.DEFAULT_KEY);
        String collect = jsonArray.stream().map(entry -> {
            JSONObject entries = JSONUtil.parseObj(entry);
            return ElParseFactory.buildJsonToEl(entries);
        }).filter(Objects::nonNull).collect(Collectors.joining(", "));
        return "PRE(" + collect + ")";
    }
}
